package com.wmh.baseservice.robot.controller;

import com.wmh.baseservice.common.ex.ServiceException;
import com.wmh.baseservice.common.utils.LogUtil;
import com.wmh.baseservice.common.utils.returnbody.ReturnBody;
import com.wmh.baseservice.robot.entity.RobotExecuteMode;
import com.wmh.baseservice.robot.entity.RobotInfo;
import com.wmh.baseservice.robot.entity.RobotStatus;
import com.wmh.baseservice.robot.entity.RobotTask;
import com.wmh.baseservice.robot.pojo.vo.PushTaskReqVO;
import com.wmh.baseservice.robot.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.ServletRequest;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author 谢骏
 * 基础管理员可访问的 自动化机器接口
 */
@Api(tags = "自动化机器-基础管理员", value = "基础管理员")
@RestController
@RequestMapping("baseAdmin/robot")
@RequiresPermissions("baseAdmin.robot")
@Validated
public class BaseAdRobotController {

    @Resource
    private ServletRequest request;

    @Resource
    private RobotInfoService robotInfoService;

    @Resource
    private RobotStatusService robotStatusService;

    @Resource
    private RobotTaskService robotTaskService;

    @Resource
    private RobotTaskTemplateService robotTaskTemplateService;

    @Resource
    private RobotExecuteModeService robotExecuteModeService;

    @ApiOperation(value = "发布任务", tags = "基础管理员")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataTypeClass = String.class, name = "robotIps", value = "机器 ID 列表(JSON)", required = true),
            @ApiImplicitParam(paramType = "query", dataTypeClass = String.class, name = "templateId", value = "模板 ID", required = true),
    })
    @PostMapping("pushTask")
    public String pushTask(PushTaskReqVO pushTaskReqVo) {
        LogUtil.info("pushTask param => {}", pushTaskReqVo);
        robotInfoService.pushTask(pushTaskReqVo);
        return ReturnBody.success();
    }

    @ApiOperation(value = "暂停任务", tags = "基础管理员")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataTypeClass = String.class, name = "ip", value = "机器 IP", required = true),
    })
    @PostMapping("stopTask")
    public String stopTask(@NotNull(message = "您没有提交该机器编号") Long robotNumber) {
        LogUtil.info("robotNumber => ip : {}", robotNumber);
        robotInfoService.stopTask(robotNumber);
        return ReturnBody.success();
    }

    @ApiOperation(value = "机器列表", tags = "基础管理员")
    @PostMapping("list")
    public String list() {
        List<RobotInfo> list = robotInfoService.list();
        return ReturnBody.success(list);
    }

    @ApiOperation(value = "获取状态列表", tags = "基础管理员")
    @PostMapping("statusList")
    public String statusList() {
        List<RobotStatus> list = robotStatusService.list();
        return ReturnBody.success(list);
    }

    @ApiOperation(value = "获取任务列表", tags = "基础管理员")
    @PostMapping("taskList")
    public String taskList() {
        List<RobotTask> list = robotTaskService.list();
        return ReturnBody.success(list);
    }

    @ApiOperation(value = "获取任务执行方式选项列表", tags = "基础管理员")
    @PostMapping("getRobotTaskExecuteModeList")
    public String getRobotTaskExecuteModeList() {
        List<RobotExecuteMode> list = robotExecuteModeService.list();
        if (list.isEmpty()) {
            throw new ServiceException("获取任务执行方式失败,请尝试刷新或联系管理员");
        }
        return ReturnBody.success(list);
    }

}
